﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MultiAnnotatedRegex.parser;
using MultiAnnotatedRegex.tree;
using MultiAnnotatedRegex.machine;
using MultiAnnotatedRegex.compiler;

namespace MultiTaggedRegex {
	public class Engine<Tag> {
		private OneOfMachine<Tag> root = new OneOfMachine<Tag>();

		private Tree Parse(string pattern) {
			IEnumerable<Token> tokenStream = new Lexer().Scan(pattern);
			return new Parser().Parse(tokenStream);
		}

		private Machine<Tag> Compile(Tree ast, Tag tag) {
			Machine<Tag> m = new Compiler().Compile<Tag>(ast);
			m.Accept.Tag = tag;
			return m;
		}

		public void RegisterPattern(string pattern, Tag tag) {
			Tree ast = Parse(pattern);
			Machine<Tag> m = Compile(ast, tag);
			root.AddAlternative(m);
		}

		public IEnumerable<Tag> GetMatchingAnnotations(string input) {
			Matcher m = new Matcher(root, input);
			return m.GetTags();
		}
	}
}
